﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;

namespace SchoolManagementSystem
{
    public partial class Main : Form
    {
        private Login loginForm = null;
        private Akun akun = null;
        private AkunAdd akunadd = null;
        private Log log = null;
        private Murid murid = null;
        private MuridAdd muridadd = null;
        private Ortu ortu = null;
        private OrtuAdd ortuadd = null;
        private Kelas kelas = null;
        private MuridBaru muridbaru = null;
        private Absensi absensi = null;
        private Karyawan karyawan = null;
        private KaryawanAdd karyawanadd = null;
        private AbsensiReport absensireport = null;
        private KategoriKeuangan kategorikeuangan = null;
        private SPP spp = null;
        private Keuangan keuangan = null;
        private Holiday holiday = null;

        public Main()
        {
            InitializeComponent();
        }

        public Main(Login loginInstance)
        {
            InitializeComponent();
            this.loginForm = loginInstance;
            akun = new Akun();
            log = new Log();
        }

        private void Main_Load(object sender, EventArgs e)
        {
           
        }

        private void Main_FormClosed(object sender, EventArgs e)
        {
            this.loginForm.Show();
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            Database.getInstance().logSistem("Logout dari sistem");
        }

        private void akunToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void logToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (log.IsDisposed)
                log = new Log();

            log.MdiParent = this;
            log.Show();
        }

        private void daftarAkunToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (akun.IsDisposed)
                akun = new Akun();

            akun.MdiParent = this;
            akun.Show();
        }

        private void tambahAkunToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.akunadd == null || this.akunadd.IsDisposed)
                akunadd = new AkunAdd();

            akunadd.MdiParent = this;
            akunadd.Show();
        }

        private void daftarMuridToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.murid == null || this.murid.IsDisposed)
                murid = new Murid();

            //check whether the form has been opened or not
            if (this.isOpenForm(murid))
            {
                murid = null;
                return;
            }

            murid.MdiParent = this;
            murid.Show();
        }

        private void tambahMuridToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.muridadd == null || this.muridadd.IsDisposed)
            {
                if (this.murid == null)
                {
                    this.murid = new Murid();
                }
                muridadd = new MuridAdd(this.murid);
            }

            //check whether the form has been opened or not
            if (this.isOpenForm(muridadd))
            {
                muridadd = null;
                return;
            }

            muridadd.MdiParent = this;
            muridadd.Show();
        }

        public Boolean isOpenForm(Form form)
        {
            foreach (Form frm in this.MdiChildren)
            {
                if (form.GetType()==frm.GetType())
                    return true;
            }

            return false;
        }

        private void daftarOrangtuaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ortu == null || this.ortu.IsDisposed)
                ortu = new Ortu();

            //check whether the form has been opened or not
            if (this.isOpenForm(ortu))
            {
                ortu = null;
                return;
            }

            ortu.MdiParent = this;
            ortu.Show();
        }

        private void tambahOrangtuaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ortuadd == null || this.ortuadd.IsDisposed)
                ortuadd = new OrtuAdd();
            
            //check whether the form has been opened or not
            if (this.isOpenForm(ortuadd))
            {
                ortuadd = null;
                return;
            }

            ortuadd.MdiParent = this;
            ortuadd.Show();
        }

        private void daftarKelasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.kelas == null || this.kelas.IsDisposed)
                kelas = new Kelas();

            //check whether the form has been opened or not
            if (this.isOpenForm(kelas))
            {
                kelas = null;
                return;
            }

            kelas.MdiParent = this;
            kelas.Show();
        }

        private void daftarMuridBaruToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.muridbaru == null || this.muridbaru.IsDisposed)
                muridbaru = new MuridBaru();

            //check whether the form has been opened or not
            if (this.isOpenForm(muridbaru))
            {
                muridbaru = null;
                return;
            }

            muridbaru.MdiParent = this;
            muridbaru.Show();
        }

        private void masukkanMuridToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.absensi == null || this.absensi.IsDisposed)
                absensi = new Absensi();

            //check whether the form has been opened or not
            if (this.isOpenForm(absensi))
            {
                absensi = null;
                return;
            }

            absensi.MdiParent = this;
            absensi.Show();
        }

        private void daftarKaryawanToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.karyawan == null || this.karyawan.IsDisposed)
                karyawan = new Karyawan();

            //check whether the form has been opened or not
            if (this.isOpenForm(karyawan))
            {
                karyawan = null;
                return;
            }

            karyawan.MdiParent = this;
            karyawan.Show();
        }

        private void tambahKaryawanToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.karyawanadd == null || this.karyawanadd.IsDisposed)
                karyawanadd = new KaryawanAdd();

            //check whether the form has been opened or not
            if (this.isOpenForm(karyawanadd))
            {
                karyawanadd = null;
                return;
            }

            karyawanadd.MdiParent = this;
            karyawanadd.Show();
        }

        private void laporanToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void daftarKaryawanToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            // ganti cursor
            Cursor = Cursors.WaitCursor;

            string query = "SELECT emp_id, emp_name, emp_gender, emp_birthplace, CONVERT(VARCHAR(50),emp_birthdate,106), emp_edu, emp_address, CONVERT(VARCHAR(50),emp_start,106) AS emp_startwork, emp_yayasan, emp_role, emp_duration FROM Employee";
            
            DataTable dTable = Database.getInstance().query(query);
            ReportDocument rptDoc = new ReportDocument();
            alwardahDataSet ds = new alwardahDataSet();

            for (int i = 0; i < dTable.Rows.Count; i++)
            {
                string buffer = dTable.Rows[i].ItemArray[3].ToString();
                buffer += ", ";
                buffer += dTable.Rows[i].ItemArray[4].ToString();
                dTable.Rows[i]["emp_birthplace"] = buffer;
            }

            ds.Tables["EmployeeReport"].Merge(dTable);
            string appPath = (System.IO.Path.GetDirectoryName(Application.ExecutablePath));
            rptDoc.Load(appPath + "\\Report\\ReportPK12.rpt");
            rptDoc.SetDataSource(ds);

            // siapkan report viewer
            ReportForm reportForm = new ReportForm(rptDoc);
            reportForm.ShowDialog();

            Cursor = Cursors.Default;
        }

        private void daftarAbsensiSiswaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.absensireport == null || this.absensireport.IsDisposed)
                absensireport = new AbsensiReport();

            //check whether the form has been opened or not
            if (this.isOpenForm(absensireport))
            {
                absensireport = null;
                return;
            }

            absensireport.MdiParent = this;
            absensireport.Show();
        }

        private void hariLiburToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.holiday == null || this.holiday.IsDisposed)
                holiday = new Holiday();

            //check whether the form has been opened or not
            if (this.isOpenForm(holiday))
            {
                holiday = null;
                return;
            }

            holiday.MdiParent = this;
            holiday.Show();
        }

        private void bukuKasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.keuangan == null || this.keuangan.IsDisposed)
                keuangan = new Keuangan();

            //check whether the form has been opened or not
            if (this.isOpenForm(keuangan))
            {
                keuangan = null;
                return;
            }

            keuangan.MdiParent = this;
            keuangan.Show();
        }

        private void sPPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.spp == null || this.spp.IsDisposed)
                spp = new SPP();

            //check whether the form has been opened or not
            if (this.isOpenForm(spp))
            {
                spp = null;
                return;
            }

            spp.MdiParent = this;
            spp.Show();
        }

        private void kategoriToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.kategorikeuangan == null || this.kategorikeuangan.IsDisposed)
                kategorikeuangan = new KategoriKeuangan();

            //check whether the form has been opened or not
            if (this.isOpenForm(kategorikeuangan))
            {
                kategorikeuangan = null;
                return;
            }

            kategorikeuangan.MdiParent = this;
            kategorikeuangan.Show();
        }
    }
}
